package com.myaihui.controller;

import com.belonk.commons.util.date.DateUtils;
import com.myaihui.domain.PMBillItem;
import com.myaihui.domain.SysUser;
import com.myaihui.foundation.model.Pager;
import com.myaihui.foundation.msg.MsgDefinition;
import com.myaihui.foundation.msg.ResultMsg;
import com.myaihui.foundation.query.PMBillItemQuery;
import com.myaihui.service.PMBillItemService;
import com.myaihui.util.LoginUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * Created by sun on 2018/9/10.
 *
 * @author sunfuchang03@126.com
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/pmbill/item")
public class PMBillItemController {
    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Static fields/constants/initializer
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    private static Logger log = LoggerFactory.getLogger(PMBillItemController.class);
    private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Instance fields
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @Autowired
    private PMBillItemService pmBillItemService;

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Constructors
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */



    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Public Methods
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @RequestMapping("/{id}")
    @ResponseBody
    public ResultMsg get(@PathVariable String id) {
        PMBillItem billItem = pmBillItemService.getById(id);
        return ResultMsg.success(billItem);
    }

    @RequestMapping("/all")
    @ResponseBody
    public ResultMsg getAll(PMBillItemQuery query) {
        List<PMBillItem> pmBillItems = pmBillItemService.queryItems(query.getBillId());
        for (PMBillItem pmBillItem : pmBillItems) {
            Date startDay = pmBillItem.getStartDay();
            Date endDay = pmBillItem.getEndDay();
            if (startDay != null && endDay != null) {
                pmBillItem.setDuration(DateUtils.of(startDay).format(formatter) + "/" + DateUtils.of(endDay).format(formatter));
            }
        }
        Pager<PMBillItem> pager = new Pager<>();
        pager.setAaData(pmBillItems);
        pager.setiTotalRecords(pmBillItems.size());
        pager.setiTotalDisplayRecords(pmBillItems.size());
        pager.setsEcho(query.getsEcho());
        return ResultMsg.success(pager);
    }

    @RequestMapping("/save")
    @ResponseBody
    public ResultMsg save(PMBillItem billItem) {
        String id = billItem.getId();
        SysUser sysUser = LoginUtil.loginUser();
        if (StringUtils.hasLength(id)) {
            PMBillItem src = pmBillItemService.getById(id);
            billItem.setCreated(src.getCreated());
            billItem.setLastModifiedTime(new Date());
            billItem.setLastModifiedUserId(sysUser.getId());
            billItem = pmBillItemService.update(billItem);
        } else {
            billItem.setCreated(new Date());
            billItem = pmBillItemService.add(billItem);
        }
        return ResultMsg.success(billItem);
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public ResultMsg delete(@PathVariable String id) {
        if (!StringUtils.hasLength(id)) {
            return ResultMsg.error(MsgDefinition.EMPTY_ARGUMENTS);
        }
        pmBillItemService.delete(id);
        return ResultMsg.success(true);
    }
    
    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * Private Methods
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */


}